package com.ztc.util;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NumberUtil {
	// 数字位
	public static String[] chnNumChar = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
	public static char[] chnNumChinese = { '零', '一', '二', '三', '四', '五', '六', '七', '八', '九' };
	// 节权位
	public static String[] chnUnitSection = { "", "万", "亿", "万亿" };
	// 权位
	public static String[] chnUnitChar = { "", "十", "百", "千" };
	public static HashMap<Character, Integer> intList = new HashMap<Character, Integer>();
	static {
		for (int i = 0; i < chnNumChar.length; i++) {
			intList.put(chnNumChinese[i], i);
		}

		for (char i = 0; i < 100; i++) {
			intList.put(new Character((char) (48 + i)), new Integer(i));
		}

		intList.put('十', 10);
		intList.put('百', 100);
		intList.put('千', 1000);

	}

	public static String numberToChinese(int num) {// 转化一个阿拉伯数字为中文字符串
		if (num == 0) {
			return "零";
		}
		int oldNum = num;
		int unitPos = 0;// 节权位标识
		String All = new String();
		String chineseNum = new String();// 中文数字字符串
		boolean needZero = false;// 下一小结是否需要补零
		String strIns = new String();
		while (num > 0) {
			int section = num % 10000;// 取最后面的那一个小节
			if (needZero) {// 判断上一小节千位是否为零，为零就要加上零
				All = chnNumChar[0] + All;
			}
			chineseNum = sectionTOChinese(section, chineseNum);// 处理当前小节的数字,然后用chineseNum记录当前小节数字
			if (section != 0) {// 此处用if else 选择语句来执行加节权位
				strIns = chnUnitSection[unitPos];// 当小节不为0，就加上节权位
				chineseNum = chineseNum + strIns;
			} else {
				strIns = chnUnitSection[0];// 否则不用加
				chineseNum = strIns + chineseNum;
			}
			All = chineseNum + All;
			chineseNum = "";
			needZero = (section < 1000) && (section > 0);
			num = num / 10000;
			unitPos++;
		}
		if(oldNum < 20 && oldNum >= 10) {
			All = All.substring(1);
		}
		return All;
	}

	private static String sectionTOChinese(int section, String chineseNum) {
		String setionChinese = new String();// 小节部分用独立函数操作
		int unitPos = 0;// 小节内部的权值计数器
		boolean zero = true;// 小节内部的制零判断，每个小节内只能出现一个零
		while (section > 0) {
			int v = section % 10;// 取当前最末位的值
			if (v == 0) {
				if (!zero) {
					zero = true;// 需要补零的操作，确保对连续多个零只是输出一个
					chineseNum = chnNumChar[0] + chineseNum;
				}
			} else {
				zero = false;// 有非零的数字，就把制零开关打开
				setionChinese = chnNumChar[v];// 对应中文数字位
				setionChinese = setionChinese + chnUnitChar[unitPos];// 对应中文权位
				chineseNum = setionChinese + chineseNum;
			}
			unitPos++;
			section = section / 10;
		}

		return chineseNum;
	}

	public static BigInteger ChineseToNumber(String str){    
        String str1 = new String();    
        String str2 = new String();    
        String str3 = new String();    
          
          
        int k = 0;    
        boolean dealflag = true;    
        for(int i=0;i<str.length();i++){//先把字符串中的“零”除去    
                if('零' == (str.charAt(i))){    
                    str = str.substring(0, i) + str.substring(i+1);    
                }    
        }   
          
        String chineseNum = str;    
        for(int i=0;i<chineseNum.length();i++){    
            if(chineseNum.charAt(i) == '亿'){    
                str1 = chineseNum.substring(0,i);//截取亿前面的数字，逐个对照表格，然后转换    
                k = i+1;    
                dealflag = false;//已经处理    
                  
                if(!chineseNum.contains("万") && chineseNum.length()>i) {  
                     str3 = str.substring(i+1,chineseNum.length());   
                }  
            }    
            if(chineseNum.charAt(i) == '万'){    
                str2 = chineseNum.substring(k,i);    
                str3 = str.substring(i+1);    
                dealflag = false;//已经处理    
            }   
           
        }    
        if(dealflag){//如果没有处理    
            str3 =  chineseNum;    
        }    
          
        BigInteger str1Int=sectionChinese(str1).multiply(new BigInteger("100000000"));  
        BigInteger str2Int=sectionChinese(str2). multiply(new BigInteger("10000"));  
        BigInteger str3Int=sectionChinese(str3);    
          
        BigInteger result =str1Int.add(str2Int).add(str3Int);  
                 
        return result;    
    }    
        
    public static BigInteger sectionChinese(String str){    
  
        int value = 0;  
          
        int sectionNum = 0;    
        for(int i=0;i<str.length();i++){    
          
            int v = (int) intList.get(str.charAt(i));  
            if(v>=0 && v<=9) {  
                if(sectionNum==0) {  
                    sectionNum=v;  
                }else {  
                    sectionNum =sectionNum*10+ v;  
                }  
                  
                if(i == str.length()-1) {  
                    value=sectionNum+value;  
                }  
            }  
              
            if(v == 10 || v == 100 || v == 1000) {  
                if(i==0) {  
                    sectionNum=1;  
                }  
                sectionNum=sectionNum*v;  
                value=sectionNum+value;  
                sectionNum=0;  
            }  
        }    
        return new BigInteger(String.valueOf(value));
    }
    
    /**
     * 利用正则表达式判断字符串是否是数字
     * @param str
     * @return
     */
    public static boolean isNumeric(String str){
		Pattern pattern = Pattern.compile("[0-9.]*");
		Matcher isNum = pattern.matcher(str);
		if( !isNum.matches() ){
		    return false;
		}
		return true;
    }

	public static void main(String[] args) {
		System.out.println(numberToChinese(1));
	}
}
